package com.jtpay.sdk.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.channel.test.tblPassageTimeStatistics;
import com.channel.test.tblPassageTimeStatisticsDao;
import com.jtpay.sdk.R;
import com.jtpay.sdk.adapter.TimerAdapetr;
import com.jtpay.sdk.view.CustomListView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.query.Query;

public class TimerListActivity extends BaseActivity implements CustomListView.OnLoadMoreListener,CustomListView.OnRefreshListener{

    private static final int OFFSETNUM=20;

    private static final int getDataSuccess = 1001;//
    private static final int getDataFail = 1002;//
    private static final int Refresh = 1003;//
    private static final int LoadMore = 1004;//
    private static final int LoadMore_NoMore = 1005;//
    private static final int LoadMore_Success = 1006;//

    private CustomListView LayoutCustomListView;
    private TimerAdapetr listAdapter;
    private List<tblPassageTimeStatistics> arrayList=new ArrayList<>();
    protected int Offset[] = { 0 };
    protected boolean More[] = { true };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_list);
        //database data init
        setupDatabase();

        initView();

        loadData(0);
    }

    private void initView() {
        LayoutCustomListView= (CustomListView) findViewById(R.id.listviewTimerlist);
        listAdapter=new TimerAdapetr(TimerListActivity.this, arrayList);
        LayoutCustomListView.setOnRefreshListener(this);
        LayoutCustomListView.setOnLoadListener(this);
    }
    /*greenDao database operation*/
    private AbstractDao getTblPassageTimeStatisticsDao() {
        return daoSession.getTblPassageTimeStatisticsDao();
    }

    private void queryDataList(){
// Query 类代表了一个可以被重复执行的查询
        Query query = getTblPassageTimeStatisticsDao().queryBuilder()
                .offset(Offset[0] * OFFSETNUM)
                .limit(OFFSETNUM)//取前n条数据
                .orderDesc(tblPassageTimeStatisticsDao.Properties.Id)
                .build();

//      查询结果以 List 返回
        List<tblPassageTimeStatistics> lists = query.list();

        arrayList.addAll(lists);

        if (Offset[0] > 1) {
            // 加载更多 成功
            Message msg = new Message();
            msg.what = LoadMore_Success;
            handler.sendMessage(msg);
        } else {
            // 刷新 成功
            Message msg = new Message();
            msg.what = getDataSuccess;
            handler.sendMessage(msg);
        }
        if (lists.size() >= OFFSETNUM) {
            Offset[0] += 1; // 偏移增加
            More[0] = true;// 布尔类型设置为true 还有更多
        } else {
            More[0] = false;// 布尔类型设置为false 没有更多了
        }
    }
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Refresh:
                    Offset[0] = 0;
                    arrayList.clear();
                    listAdapter.notifyDataSetChanged();
                    queryDataList();
                    break;
                case LoadMore:
                    listAdapter.notifyDataSetChanged();
                    queryDataList();
                    break;
                case LoadMore_Success:
                    LayoutCustomListView.onRefreshComplete();
                    LayoutCustomListView.onLoadMoreComplete();

                    listAdapter.notifyDataSetChanged();
                    break;
                case LoadMore_NoMore:
                    LayoutCustomListView.onRefreshComplete();
                    LayoutCustomListView.onLoadMoreComplete();

                    Toast.makeText(TimerListActivity.this, "没有更多了",Toast.LENGTH_SHORT).show();
                    break;
                case getDataSuccess:
                    LayoutCustomListView.onRefreshComplete();
                    LayoutCustomListView.onLoadMoreComplete();

                    LayoutCustomListView.setAdapter(listAdapter);
                    LayoutCustomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(TimerListActivity.this, TimerDetailActivity.class);
                            intent.putExtra("passageData",arrayList.get(position-1));
                            startActivity(intent);
                        }
                    });

                    break;
                case getDataFail:
                    LayoutCustomListView.onRefreshComplete();
                    LayoutCustomListView.onLoadMoreComplete();
                    Toast.makeText(TimerListActivity.this, "获取数据出错",Toast.LENGTH_SHORT).show();
                    break;

            }
            super.handleMessage(msg);
        }

    };

    @Override
    public void onLoadMore() {
        loadData(1);
    }

    @Override
    public void onRefresh() {
        loadData(0);
    }

    // 加载列表数据
    public void loadData(final int type) {
        switch (type) {
            case 0:// 刷新数据
                Message msg = new Message();
                msg.what = Refresh;
                handler.sendMessage(msg);
                break;
            case 1:// 加载更多
                if (More[0]) {// 还有更多
                    msg = new Message();
                    msg.what = LoadMore;
                    handler.sendMessage(msg);
                } else {
                    msg = new Message();
                    msg.what = LoadMore_NoMore;
                    handler.sendMessage(msg);
                }
                break;
        }
    }
}
